﻿using System;

namespace TypeContravariance
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("===================测试泛型逆变=================");
            //多态
            IPushable<Animal> animals = new Stack<Animal>();
            //Dogs指向的是Animals
            IPushable<Dog> dogs = animals;
            dogs.Push(new Dog());
            //dogs.Push(new Cat());
      
        }
    }

    public class Animal{};
    public class Dog : Animal { };
    public class Cat : Animal { };


    //实现两个接口 
    //同时Input可以使用逆变
    public class Stack<T> : IStack<T>, IPushable<T>
    {
        T[] values = new T[100];
        int position = 0;
        public T GetT
        {
            get => values[position];
            set => values[position] = value;
        }

        public void Push(T value)
        {
            values[++position] = value;
        }
        public T Pop()
        => values[position--];

    }
    public interface IPushable<in T>
    {
        void Push(T value);
    }
    public interface IStack<T>
    {
        T GetT { get; set; }
        T Pop();
    }

}
